package com.lilili.ssh.login.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.lilili.ssh.base.BaseAction;
import com.lilili.ssh.book.model.Book;
import com.lilili.ssh.book.service.BookManager;
import com.lilili.ssh.login.model.SysInfo;
import com.lilili.ssh.login.service.LoginManager;
import com.lilili.ssh.relation.model.Relation;
import com.lilili.ssh.relation.service.RelationManager;
import com.lilili.ssh.user.model.User;
import com.lilili.ssh.user.service.UserManager;

public class LoginAction extends BaseAction{
    //Autowired自动匹配注入
	@Autowired
	LoginManager loginManager;
	@Autowired
	UserManager userManager;
	@Autowired
	BookManager bookManager;
	@Autowired
	RelationManager relationManager;
	private SysInfo sysInfo;
	
	public SysInfo getSysInfo() {
		return sysInfo;
	}
	public void setSysInfo(SysInfo sysInfo) {
		this.sysInfo = sysInfo;
	}
    //登陆校验
	public String chkLogin(){
		User user=sysInfo.getUser();
		if(user!=null){
			user=loginManager.chkLogin(user);
			if(user.getId()!=null){
				Relation relation=new Relation();
				this.getRequest().getSession().setAttribute("loginUser", user);
				if(user.getStatu()!=0){
					relation.setUser(user);
				}
				sysInfo=loginManager.getSysInfo();
				sysInfo.setUser(user);
				sysInfo.setRelationList(relationManager.getRelationList(relation));
				this.getRequest().getSession().setAttribute("sysInfo", sysInfo);
				return SUCCESS;
			}
		}
		this.getRequest().setAttribute("loginError", "用户名或者密码错误！");
		return this.INPUT;
	}
	//借书action
	public String lendBook() {
        HttpServletRequest request = this.getRequest();
        String idStr = request.getParameter("idStr");
        String userId= request.getParameter("userId");
        if (StringUtils.isNotEmpty(idStr)) {
            String [] ids=idStr.split(",");
            for(String s:ids){
            	Relation relation=new Relation();
            	Book book=bookManager.getBook(s);
            	User user=userManager.getUser(userId);
            	relation.setBook(book);
            	relation.setBookname(book.getName());
            	relation.setUser(user);
            	relation.setUsername(user.getName());
            	relationManager.saveRelation(relation);
            	book.setStatu(-1);
            	bookManager.saveBook(book);
            }
        }
        sysInfo=loginManager.getSysInfo();
		sysInfo.setRelationList(relationManager.getRelationList());
		this.getRequest().getSession().setAttribute("sysInfo", sysInfo);
        return SUCCESS;
    }
	//还书action
	public String retuBook() {
        HttpServletRequest request = this.getRequest();
        String idStr = request.getParameter("idStr");
        String userId= request.getParameter("userId");
        if (StringUtils.isNotEmpty(idStr)) {
            String [] ids=idStr.split(",");
            for(String s:ids){
            	Book book=bookManager.getBook(s);
            	relationManager.removeRelation(s);
            	book.setStatu(1);
            	bookManager.saveBook(book);
            }
        }
        sysInfo=loginManager.getSysInfo();
		sysInfo.setRelationList(relationManager.getRelationList());
		this.getRequest().getSession().setAttribute("sysInfo", sysInfo);
        return SUCCESS;
    }
	//退出系统
	public String exit(){
		this.getRequest().getSession().invalidate();
		return this.Result("index.jsp");
	}
	//用户管理
	public void viewUser() throws IOException{
		PrintWriter writor=this.getResponse().getWriter();
		List<User> pojoList=userManager.getUserList();
		String innerHTML="<table id='tabEle' border=1 cellspacing=0 width=380px align=center>";
		innerHTML=innerHTML+"<tr bgcolor='#6A95B8'>" +
				"<td colspan=3 style='border-right-width:0'>用户信息</td>" +
				"<td colspan=3 align=right style='border-left-width:0'>" +
				"<input name='add' type='button' value='新增' onclick='addUser();'/>" +
				"<input name='edit' type='button' value='编辑' onclick='editUser();'/>" +
				"<input name='del' type='button' value='删除' onclick='delUser();'/></td></tr>" +
				"<tr><td>序号</td><td>ID</td><td width=70>姓名</td><td width=70>登录名</td>" +
				"<td>注册日期</td><td>用户类型</td></tr>";
		int i=1;
		for(User u:pojoList){
			String statu="";
			if(u.getStatu()!=null){
				if(u.getStatu()==1){
					statu="普通用户";
				}else if(u.getStatu()==-1){
					statu="过期用户";
				}else if(u.getStatu()==0){
					statu="管理员";
				}
			}else{
				statu="未知";
			}
			innerHTML=innerHTML+"<tr><td>"+i+"</td>" 
			+"<td><input type='checkBox' value="+u.getId()+" name='id'/>"
			+"<td>"+u.getName()+"</td>"
			+"<td>"+u.getLoginName()+"</td>"
			+"<td>"+u.getRegDate()+"</td>"
			+"<td>"+statu+"</td></tr>";
			i++;
		}
		innerHTML=innerHTML+"</table>";
		writor.write(innerHTML);
		writor.close();
	}
	//书籍管理
	public void viewBook() throws IOException{
		PrintWriter writor=this.getResponse().getWriter();
		List<Book> pojoList=bookManager.getBookList();
		List<User> userList=userManager.getUserList();
		String comBox="读者:<select name='lendTo'>";
		for(User u:userList){
			comBox+="<option value='"+u.getId()+"' label='"+u.getName()+"'></option>";
		}
		comBox+="</select>  ";
		String innerHTML="<table id='tabEle' border=1 cellspacing=0 width=380px align=center>";
		innerHTML=innerHTML+"<tr bgcolor='#6A95B8'>" +
				"<td colspan=2 style='border-right-width:0'>书籍信息</td>" +
				"<td colspan=4 align=right style='border-left-width:0'>" +comBox+
				"<input name='lend' type='button' value='借书' onclick='lendBook();'/>" +
				"<input name='retu' type='button' value='归还' onclick='retuBook();'/>" +
				"<input name='add' type='button' value='新增' onclick='addBook();'/>" +
				"<input name='edit' type='button' value='编辑' onclick='editBook();'/>" +
				"<input name='del' type='button' value='删除' onclick='delBook();'/></td></tr>" +
				"<tr><td>序号</td><td>ID</td><td width=100>书名</td><td width=70>作者</td>" +
				"<td>注册日期</td><td>状态</td></tr>";
		int i=1;
		for(Book u:pojoList){
			String statu="";
			if(u.getStatu()!=null){
				if(u.getStatu()==1){
					statu="在库";
				}else if(u.getStatu()==-1){
					statu="借出";
				}else if(u.getStatu()==0){
					statu="废弃";
				}
			}else{
				statu="未知";
			}
			innerHTML=innerHTML+"<tr><td>"+i+"</td>" 
			+"<td><input type='checkBox' value="+u.getId()+" name='id'/>"
			+"<td>"+u.getName()+"</td>"
			+"<td>"+u.getAuthor()+"</td>"
			+"<td>"+u.getRegDate()+"</td>"
			+"<td>"+statu+"</td></tr>";
			i++;
		}
		innerHTML=innerHTML+"</table>";
		writor.write(innerHTML);
		writor.close();
	}
	
}
